﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RESP_IIWaterAssessment
{
    public partial class MeetingAVPForm : Form
    {
        public MeetingAVPForm()
        {
            InitializeComponent();
        }

        bool isConflict;
        tb_meeting meeting;
        tb_conflict conflict;
        int avpID, regID, mTypeID, mResultValue;

        private void MeetingAVPForm_Load(object sender, EventArgs e)
        {
            cbRegion.SelectedIndexChanged -= cbRegion_SelectedIndexChanged;
            cbRegion.DataSource = UtilClass.FillCBRegions();
            cbRegion.DisplayMember = "Name";
            cbRegion.ValueMember = "ID";

            if (InformationClass.isAdmin)
            {
                cbRegion.SelectedIndex = -1;
                cbRegion.Text = string.Empty;
                cbRegion.SelectedIndexChanged += new EventHandler(cbRegion_SelectedIndexChanged);
            }
            else
            {
                cbRegion.Enabled = false;
                regID = InformationClass.regionID;
                cbRegion.SelectedIndexChanged += new EventHandler(cbRegion_SelectedIndexChanged);
                cbRegion.SelectedValue = regID;
                cbRegion_SelectedIndexChanged(cbRegion, null);
                cbAVP.Enabled = true;
                cbAVP.Text = string.Empty;
                toolStrip1.Visible = false;
            }

            cbMeetingType.Items.AddRange(UtilClass.GetMeetingTyps());
        }



        private void cbRegion_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbAVP.SelectedIndexChanged -= cbAVP_SelectedIndexChanged;
            regID = Convert.ToInt32(cbRegion.SelectedValue);
            cbAVP.Enabled = true;
            //cbAVP.Items.Clear();
            cbAVP.Text = string.Empty;
            cbAVP.DataSource = UtilClass.FillAVP(regID);
            cbAVP.DisplayMember = "Name";
            cbAVP.ValueMember = "ID";
            cbAVP.SelectedIndex = -1;
            cbAVP.Text = string.Empty;
            cbAVP.SelectedIndexChanged += new EventHandler(cbAVP_SelectedIndexChanged);
        }

        private void cbAVP_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbAVP.SelectedIndex >= 0)
            {
                cbMeetingType.Enabled = tbQuestions.Enabled = tbNote.Enabled = dtpDate.Enabled = true;
                avpID = Convert.ToInt32(cbAVP.SelectedValue);
            }
        }

        private void cbMeetingType_SelectedIndexChanged(object sender, EventArgs e)
        {
            mTypeID = GetMeetingID(cbMeetingType.Items[cbMeetingType.SelectedIndex].ToString());
            if (ShowVisitors(mTypeID))
            {
                tbVisitors.Enabled = true;
            }
            else
            {
                tbVisitors.Enabled = false;
            }

            isConflict = DBClass.MeetingTypes.Where(wh => wh.ID == mTypeID).Select(se => se.IsConflict).SingleOrDefault();
        }

        private bool ShowVisitors(int mTypeID)
        {
            return DBClass.MeetingTypes.Where(wh => wh.ID == mTypeID).Select(se => se.Visitors).SingleOrDefault();
        }

        private int GetMeetingID(string mName)
        {
            if (InformationClass.Lang)
                return DBClass.MeetingTypes.Where(wh => wh.NameRus == mName).Select(se => se.ID).SingleOrDefault();
            else
                return DBClass.MeetingTypes.Where(wh => wh.NameUzb == mName).Select(se => se.ID).SingleOrDefault();
        }

        private void cbResult_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            //**************8
            if (cbAVP.SelectedIndex >= 0 && cbMeetingType.SelectedIndex >= 0)
            {
                meeting = new tb_meeting
                {
                    ID = GetNextID(avpID),
                    AVPID = avpID,
                    Date = dtpDate.Value.Date,
                    Note = tbNote.Text,
                    Questions = tbQuestions.Text,
                    Results = rbResultTrue.Checked,
                    TypeID = GetMeetingID(cbMeetingType.Items[cbMeetingType.SelectedIndex].ToString())
                };
                if (tbVisitors.Enabled)
                    meeting.Visitors = tbVisitors.Text;

                DBClass.RESPContext.tb_meetings.InsertOnSubmit(meeting);
                DBClass.RESPContext.SubmitChanges();
                if (isConflict)
                {
                    ConflictForm cf = new ConflictForm(meeting.ID, meeting.AVPID);
                    if (cf.ShowDialog() != DialogResult.OK)
                    {
                        if (InformationClass.Lang)
                            MessageBox.Show("Не заполнена информация о конфликте");
                        else
                            MessageBox.Show("Низо ҳақида маълумот тўлдирилмаган");
                        return;
                    }

                    conflict = cf.Conflict;
                    DBClass.RESPContext.tb_conflicts.InsertOnSubmit(conflict);
                }

                try
                {
                    DBClass.RESPContext.SubmitChanges();
                    MessageBox.Show(msgClass.msgSubmitChanges, msgClass.msgAchtung);
                }
                catch
                {
                    MessageBox.Show(msgClass.msgError, msgClass.msgAchtung);
                    DBClass.Meetings.DeleteOnSubmit(meeting);
                    if (conflict != null && isConflict)
                        DBClass.Conflicts.DeleteOnSubmit(conflict);
                    DBClass.RESPContext.SubmitChanges();
                }
                this.Close();
            }
            else
            {
                if (InformationClass.Lang)
                    MessageBox.Show("Не выбраны все необходимые к заполнению поля");
                else
                    MessageBox.Show("Тўлдирилиши керак бўлган барча сатрлар тўлдирилмаган ");
            }
        }

        private int GetNextID(int aID)
        {
            try
            {
                return DBClass.Meetings.Where(wh => wh.AVPID == aID).Max(m => m.ID) + 1;
            }
            catch
            {
                return 1;
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void butAllMeetings_Click(object sender, EventArgs e)
        {
            ViewMeetingsForm viewMeetings = new ViewMeetingsForm(false);
            viewMeetings.ShowDialog();
        }


    }
}
